﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;
using System.IO;

//using MvcDatagrid.Models;

namespace Helpers
{
    public static class HtmlHelperExtensions
    {
        //public static MvcHtmlString ImagePager(this HtmlHelper helper, IEnumerable<Images> images)
        //{
        //    TagBuilder builder = new TagBuilder("img");
        //    string htmlToRender = builder.ToString(TagRenderMode.SelfClosing);
        //    return new MvcHtmlString(htmlToRender);
        //}
        private static TagBuilder getImgInDiv(string imgSrc, string divClass)
        {
            TagBuilder container = new TagBuilder("div");
            if (!string.IsNullOrEmpty(divClass))
            {
                container.AddCssClass(divClass);
            }
            TagBuilder builder = new TagBuilder("img");
            builder.Attributes.Add("src", imgSrc);
            container.InnerHtml += builder.ToString(TagRenderMode.SelfClosing);
            return container;
        }

        public static MvcHtmlString Image(this HtmlHelper helper,
                                string url)
        {
            TagBuilder editTool = getImgInDiv("/Images/Test/pen_edit.png", "edit-tool");
            TagBuilder delTool = getImgInDiv("/Images/Test/close_delete.png", "del-tool");
            TagBuilder editTools = new TagBuilder("div");
            editTools.AddCssClass("edit-tools");
            editTools.InnerHtml += editTool.ToString();
            editTools.InnerHtml += delTool.ToString();

            TagBuilder imgContainer = getImgInDiv(url, "img-container");

            TagBuilder container = new TagBuilder("div");
            container.AddCssClass("img-display");
            container.InnerHtml += editTools.ToString();
            container.InnerHtml += imgContainer.ToString();
            return new MvcHtmlString(container.ToString());
        }

        public static string RenderPartialView(this Controller controller, string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                return null;

            //if (string.IsNullOrEmpty(viewName))            
            //    viewName = controller.ControllerContext.RouteData.GetRequiredString("action"); 

            controller.ViewData.Model = model;

            using (var sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                var viewContext = new ViewContext(controller.ControllerContext, 
                                                    viewResult.View, 
                                                    controller.ViewData, 
                                                    controller.TempData, sw);
                viewResult.View.Render(viewContext, sw); 
                return sw.GetStringBuilder().ToString();
            }
        }

        //public static MvcHtmlString DisplayImagesBySix(this HtmlHelper helper, IEnumerable<Images> images
    }
}
